<style>
.layui-form-pane .layui-form-label{width:auto;}
.layui-form-item{margin-bottom: 0px;}
.order-time-bg{background-color:#1E9FFF;color:white;padding:0px 5px;}
</style>
<div class="fixed-bar">
<form class="layui-form layui-form-pane fixed-bar-form" action="{:url()}" method="get" id="hisi-table-search-optable">
<div class="layui-form-item">
	<div class="layui-inline">
		<label class="layui-form-label">订单号</label>
		<div class="layui-input-inline">
			<input type="text" name="client_ordernum" lay-verify="" placeholder="输入订单号" class="layui-input">
		</div>
	</div>
	{if env('env_site') == 'riwei' }
	<div class="layui-inline">
		<label class="layui-form-label">客户单号</label>
		<div class="layui-input-inline">
			<input type="text" name="ext_id" lay-verify="" placeholder="输入客户单号" class="layui-input">
		</div>
	</div>
	{/if}
	<div class="layui-inline">
		<label class="layui-form-label">SKU</label>
		<div class="layui-input-inline">
			<input type="text" name="sku" lay-verify="" placeholder="输入SKU" class="layui-input">
		</div>
	</div>
	<div class="layui-inline">
		<label class="layui-form-label">所属工厂</label>
		<div class="layui-input-inline">
			{:Form::select('factory_id', '', $factoryKV, '', '')}
		</div>
	</div>
	<div class="layui-inline">
		<label class="layui-form-label">订单状态</label>
		<div class="layui-input-inline">
			{:Form::select('order_status', '', ['' => '请选择', 1=>'未采购', 2=>'已采购', 3=>'已裁床', 4=>'已车缝', 5=>'已后整', 6=>'已完成'], '', '')}
		</div>
	</div>
	<div class="layui-inline">
		<label class="layui-form-label">下单日期</label>
		<div class="layui-input-inline">
			<input type="text" name="create_date_range" readonly="" id="create_date_range" lay-verify="" placeholder="选择下单日期" class="layui-input">
		</div>
	</div>
	<div class="layui-inline">
		<label class="layui-form-label">完成日期</label>
		<div class="layui-input-inline">
			<input type="text" name="over_date_range" readonly="" id="over_date_range" lay-verify="" placeholder="选择完成日期" class="layui-input">
		</div>
	</div>
	<div class="layui-inline">
		<label class="layui-form-label">断码筛选</label>
		<div class="layui-input-inline">
			{:Form::select('break_type', '', [''=>'全部','1'=>'正常','2'=>'将断码','3'=>'已断码'],'','')}
		</div>
	</div>
	<div class="layui-inline">
		<button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">搜索</button>
	</div>
</div>
</form>
</div>

<table id="dataTable" lay-filter='dataTable'></table>

{include file="system@block/layui" /}

<script type="text/html" id="picTpl">
    <span class="lay-pic-container" src="{{ d.sInfo.pic }}">
		<img src="{{ d.sInfo.ori_pic }}" class="lay-pic">
		{{# if(d.status==99){ }}
			<span class="sign"><img src="/static/system/image/sign_delete.png"></span>
		{{# }else if(d.status==2){ }}
			<span class="sign"><img src="/static/system/image/sign_over.png"></span>
		{{# } }}
	</span>
</script>
<script type="text/html" id="skuTpl">
	{{# if(d.break_code != ''){ }}
	<span class="red">{{ d.break_code }}</span>
	<br>
	{{# } }}
    <span class="copy-item">{{ d.client_ordernum }}</span> <span class="layui-badge layui-bg-blue">{{ layui.tool.formatOrderOType(d.o_type) }}</span><span class="layui-badge layui-bg-orange">{{ layui.tool.formatOrderType(d.type) }}</span><br>
	{if env('env_site') == 'riwei' }
	<span class="copy-item">{{ d.ext_id }}</span> <br>
	{/if}
	<span class="copy-item">{{ d.sku }}</span> <br>
    {{ d.shipment_time_diff }} <br>
    {{ d.create_time_diff }} <br>
    {{# if(d.status == 99){ }}<span class="layui-badge">{{ d.delete_text }}</span> {{# } }}
</script>
<script type="text/html" id="factoryTpl">
    {{ d.factory_name }} <br>
    {{ d.group_name }} <br>
	{{# if(d.outgo_status > 0){ }}
		外发：{{ d.factory_outgo_name }}
	{{# } }}
</script>
<script type="text/html" id="orderNumTpl">
	{{# for(var i in d.osList){ }}
    	{{ d.osList[i].size_name }}： {{ d.osList[i].order_count }} <br>
	{{# } }}
	总数：{{ d.osStat.order_count }}
</script>
<script type="text/html" id="skuStockTpl">
	{{# if(d.skuStock.length>0){ var all_stock_count=0; }}
		<div style="writing-mode: tb-rl;float:left;width:28px;padding-top:10px;letter-spacing:4px;font-weight:bold;color:red;">可用库存</div>
		<div style="margin-left:30px;">
		{{# for(var i in d.skuStock){ all_stock_count+=parseInt(d.skuStock[i].available_count); }}
			{{ d.skuStock[i].size_name }}： {{ d.skuStock[i].available_count }} <br>
		{{# } }}
			总：{{all_stock_count}}
		</div>
	{{# } }}
</script>
<script type="text/html" id="materialTpl">
	{{ layui.tool.formatMaterialType(d.mInfo.type, '', 2) }}
	<a href="{:url('plan')}?mid={{ d.mid }}" class="hisi-iframe" hisi-data="{width: '90%', height: '85%'}" title="面料计划 - {{ d.mInfo.name }}" style="text-decoration: underline;"> {{ d.mInfo.name }} /  {{ d.mInfo.material_sku}} </a>
</script>
<script type="text/html" id="rejects_lengthTpl">
	{{#if(d.rejects_length_sum>0){}}
	<span style="color:red;">{{d.rejects_length_sum}}</span>
	{{#}}}
</script>
<script type="text/html" id="lengthTpl">
	{{# if(d.shrink_length > 0){ }}
		<a href="{:url('checkclothShrink/showLog')}?order_material_id={{ d.mid }}&type=2" class="hisi-iframe" hisi-data="{width: '90%', height: '85%'}" title="缩水明细">{{ d.shrink_length }}</a>
	{{# } else if(d.checkcloth_length > 0){ }}
		<a href="{:url('checkclothShrink/showLog')}?order_material_id={{ d.mid }}&type=1" class="hisi-iframe" hisi-data="{width: '90%', height: '85%'}" title="验布明细">{{ d.checkcloth_length }}</a>
	{{# } }}
</script>
<script type="text/html" id="operateTpl">
	<div class="operate-{{ d.mid }}">
	{{# if(d.caichuang_status == 1){ }}
		<a href="{:url('audit')}?mid={{ d.mid }}&status=3" data-id="{{ d.p.id }}" class="layui-btn layui-btn-sm hisi-ajax" confirm="您确定要执行该操作吗？" refresh="false" callback="callbackAudit">审核通过</a><br>
		<a href="{:url('audit')}?mid={{ d.mid }}&status=2" data-id="{{ d.p.id }}" class="layui-btn layui-btn-primary layui-btn-sm hisi-ajax" confirm="您确定要执行该操作吗？" refresh="false" callback="callbackAudit">重置</a><br>
	{{# } else if(d.caichuang_status == 2){ }}
		<i class="layui-icon layui-icon-close red">审核不通过</i>
	{{# } else if(d.caichuang_status == 3){ }}
		{{# if(d.caichuang_pull_status == 1){ }}
			<i class="layui-icon layui-icon-face-smile-b green">裁剪已完成</i>
		{{# } else{ }}
			<i class="layui-icon layui-icon-face-smile-b green">已审核待裁剪</i>
		{{# } }}
	{{# } else{ }}
		<i class="layui-icon layui-icon-help yellow">未填计划</i>
	{{# } }}
	</div>
</script>
<script type="text/html" title="余料使用量" id="subMaterialUseTpl">
	{{# if(d.mInfo.type=='accessories'){ }}
		{{ d.stock_use_num }}
	{{# }else{ }}
		{{# if(d.have_metres>0 || d.used_metres>0 ){ }}
			<a href="{:url('materialStock/apply')}?material_id={{ d.material_id }}&client_ordernum={{ d.p.client_ordernum }}&material_type=private" title="余料占用" class="layui-btn layui-btn-normal layui-btn-sm hisi-iframe" hisi-data="{width: '800px', height: '500px'}">占用</a>
		{{# } }}
		{{ d.used_metres }} / {{ d.have_metres }}
	{{# } }}
</script>
<script type="text/html" id="buttonTpl">
    <a href="{:url('mergeCut')}?sku={{ d.sku }}" title="同款合裁" class="layui-btn layui-btn-primary layui-btn-sm hisi-iframe" hisi-data={width:'70%',height:'80%'}>同款合裁</a><br>
	<div style="line-height:35px;" id="back{{ d.id}}">
		<a href="{:url('back')}?order_id={{ d.id }}" title="弹回采购" class="layui-btn layui-btn-primary layui-btn-sm hisi-tr-del" data-del="#child{{ d.id }}" confirm="您确定要执行该操作？">弹回采购</a><br>
	</div>
</script>

<script type="text/javascript">
	var systemUserKV = {:json_encode($systemUserKV);};
	systemUserKV['0'] = '请选择';
	var caichuangTypeKV = {:json_encode($caichuangTypeKV);};
	caichuangTypeKV['0'] = '请选择';
	
	layui.use([ 'opTable', 'jquery', 'tool', 'laydate', 'layer'], function() {
		var $ = layui.jquery;
		var laydate = layui.laydate;
		var layer = layui.layer;
		
		laydate.render({
		    elem: '#create_date_range'
		    ,range: '~'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
	    	,change: function(value, date, endDate){
		    	$('#create_date_range').val(value);
            	$('#layui-laydate1').remove();//关闭面板
            }
		});
		laydate.render({
		    elem: '#over_date_range'
		    ,range: '~'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
		    ,change: function(value, date, endDate){
		    	$('#over_date_range').val(value);
            	$('#layui-laydate2').remove();//关闭面板
            }
		});
		
		var opTable = layui.opTable.render({
			elem : '#dataTable'
			,url : '{:url()}'
			,page: true
			,limit: 20
            ,limits: [20,50,100,150,200]
            ,text: {
                none : '暂无相关数据'
            }
			,toolbar: '#toolbar'
	        ,defaultToolbar: ['filter', 'exports', 'print']
			,cols : [[ 
				{field:'pic',title : '图片', templet:'#picTpl',width:110}
				,{field:'client_ordernum',title : '订单号/SKU/时效', templet:'#skuTpl',width:200}
				,{field:'factory_id',title : '工厂/分组', templet:'#factoryTpl',width:150}
				,{field:'order_num',title : '下单件数', templet:'#orderNumTpl',width:140}
				,{field:'order_num',title : '可用库存', templet:'#skuStockTpl',width:130}
				,{field:'caichuang_remark',title : '裁床备注',edit:'text',style:'border-bottom:2px dotted #5FB878;'}
				,{field:'btn',title : '操作',templet:'#buttonTpl',width:100}
			]]

			,onEdit: function(obj) {
				var value = obj.value //得到修改后的值
			    ,data = obj.data //得到所在行所有键值
			    ,field = obj.field; //得到字段
			    
			    // 编辑备注
			    if (field == 'caichuang_remark') {
			    	$.ajax({
			 		   type: "POST",
			 		   url: "{:url('remark')}",
			 		   data: "order_id="+data.id+"&remark="+value,
			 		   dataType:"json",
			 		   success: function(res){
			 			  if (res.code == 1) {
							 layer.msg(res.msg, {icon: 1});
						  } else {
							 layer.msg(res.msg, {icon: 2,anim: 6});
						  }
			 		   }
			 		});
			    }
			}
			
			//  展开的列配置
			,openTable: function (itemData) {
				return {
					elem: '#child' + itemData.id
					,id: 'child' + itemData.id
					,data: layui.tool.setDataToList(itemData, itemData.omList)
					,cellMinWidth: 100
					,toolbar: false
				    ,defaultToolbar: ['filter', 'exports', 'print']
					,cols: [[
						{field: 'name', title: '材料名称',width:220,templet:"#materialTpl"}
						,{field:'color',title : '颜色',templet:function(d){return d.mInfo.color;}}
						,{field:'color',title : '余料已/未使用',templet:'#subMaterialUseTpl',width:126}
						,{field:'tiaoshu',title : '条数',width:60}
						,{field:'true_fabric_width',title : '缩后布封'}
						,{field:'caichuang_wheat_low',title : '低层数',templet:function(d){return '<div class="low-val-'+d.mid+'">'+d.caichuang_wheat_low+'</div>'}}
						,{field:'caichuang_wheat_height',title : '高层数',templet:function(d){return '<div class="height-val-'+d.mid+'">'+d.caichuang_wheat_height+'</div>'}}
						,{field:'caichuang_strip_dosage',title : '预留捆条', templet:function(d){return '<div class="strip-dosage-val-'+d.mid+'">'+layui.tool.bcmul(d.caichuang_strip_dosage*d.yingcai_count, 1.1)+'</div>';}}
						,{field:'yingcai_count',title : '应裁数',templet:function(d){return '<div class="yingcai-val-'+d.mid+'">'+d.yingcai_count+'</div>'}}
						,{field:'shrink_length',title : '缩后数量',templet:'#lengthTpl'}
						,{field:'length',title : '库存'}
						,{field:'billing_caigou_count',title : '开单采购量'}
						,{field:'rejects_length_sum',title : '次品数' ,templet:"#rejects_lengthTpl"}
						,{field:'caichuang_remark',title : '备注',edit:'text',style:'border-bottom:2px dotted #5FB878;'}
						,{field:'',title : '操作',templet:"#operateTpl"}
					]]
					,done:function(){
						// 监听子表单元格修改
			            layui.table.on('edit(' + 'child' + itemData.id + ')', function (obj) {
			            	var value = obj.value //得到修改后的值
						    ,data = obj.data //得到行对象
						    ,field = obj.field; //得到字段

						    // 编辑备注
						    if (field == 'caichuang_remark') {
						    	$.ajax({
						 		   type: "POST",
						 		   url: "{:url('remark')}",
						 		   data: "order_id="+data.order_id+"&remark="+value+"&mid="+data.mid,
						 		   dataType:"json",
						 		   success: function(res){
						 			  if (res.code == 1) {
										 layer.msg(res.msg, {icon: 1});
									  } else {
										 layer.msg(res.msg, {icon: 2,anim: 6});
									  }
						 		   }
						 		});
						    }
			            });
					}
		        }
		      }
			,openType : 1
			,done: function(res, curr, count){
				opTable.openAll();
			}
		});
		
		// 审核回调
		window.callbackAudit = function(obj, res) {
			console.log(res);
			if (res.code == 1) {
				var mid = res.data.mid;
				if (res.data.status == 3) { // 审核通过
					if (res.data.isAllOver == 2) {	// 全部审核通过
						var id = obj.attr('data-id');
						$('#child'+id).parents('tr').remove();
						$('#back'+id).parents('tr').remove();
					} else {
						$('.operate-'+mid).html('<i class="layui-icon layui-icon-face-smile-b green">已审核待裁剪</i>');
					}
				} else {	// 重置
					$('.operate-'+mid).html('<i class="layui-icon layui-icon-close red">审核不通过</i>');
				}
			}
		};
		
		// 面料计划录入回调
		window.callbackPlan = function(obj, res) {
			if (res.code == 1) {
				var mid = res.data.mid;
				var id = res.data.orderId;
				$(".low-val-"+mid).html(res.data.caichuang_wheat_low);
				$(".height-val-"+mid).html(res.data.caichuang_wheat_height);
				$(".strip-dosage-val-"+mid).html(layui.tool.bcmul(res.data.caichuang_strip_dosage*res.data.yingcai_count, 1.1));
				$(".yingcai-val-"+mid).html(res.data.yingcai_count);
				$('.operate-'+mid).html('<a href="{:url(\'audit\')}?mid='+mid+'&status=3" data-id="'+id+'" class="layui-btn layui-btn-sm hisi-ajax" confirm="您确定要执行该操作吗？" refresh="false" callback="callbackAudit">审核通过</a><br><a href="{:url(\'audit\')}?mid='+mid+'&status=2" data-id="'+id+'" class="layui-btn layui-btn-primary layui-btn-sm hisi-ajax" confirm="您确定要执行该操作吗？" refresh="false" callback="callbackAudit">重置</a><br>');
			}
		};
		
		/**
	     * layui非静态table搜索渲染
	     * @attr data-table table容器ID
	     * @attr hisi-data table基础参数
	     * @attr action 搜索请求地址
	     */
	   $(document).on('submit', '#hisiSearchOpTable,#hisi-table-search-optable', function() {
	        var that = $(this), 
	            arr = that.serializeArray(), 
	            where = new Array(),
	            dataTable = that.attr('data-table') ? that.attr('data-table') : 'dataTable',
	            options = new Function('return '+ that.attr('hisi-data'))() || {page: {curr:1}};
	        
	            for(var i in arr) {
	                where[arr[i].name] = arr[i].value;
	            }
	        
	        options.url = that.attr('action');
	        options.where = where;
	        
	        opTable.reload(options);
	        return false;
	    });
		
	});
</script>